
package com.bj58.disconf.web.controllers;

import javax.validation.Valid;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baidu.disconf.core.common.constants.Constants;
import com.baidu.disconf.core.common.json.ValueVo;
import com.bj58.disconf.web.PrintBeat;
import com.bj58.disconf.web.common.annotation.NoAuth;
import com.bj58.disconf.web.common.constant.WebConstants;
import com.bj58.disconf.web.controllers.config.dto.ConfigFullModel;
import com.bj58.disconf.web.service.zookeeper.config.ZooConfig;
import com.bj58.disconf.web.service.zookeeper.form.ZkDeployForm;
import com.bj58.disconf.web.service.zookeeper.service.ZkDeployMgr;
import com.bj58.disconf.web.utils.GuiceDI;
import com.bj58.wf.mvc.ActionResult;
import com.bj58.wf.mvc.annotation.Path;

/**
 * Zoo API
 *
 * @author lemon
 * @version 2014-1-20
 */
@Controller
@RequestMapping(WebConstants.API_PREFIX + "/zoo")
@Path("/api/zoo")
public class ZooController extends BaseController {

    protected static final Logger LOG = LoggerFactory.getLogger(ZooController.class);

    private final ZooConfig zooConfig = GuiceDI.getInstance(ZooConfig.class);

    private final ZkDeployValidator zkDeployValidator = GuiceDI.getInstance(ZkDeployValidator.class);

    private final ZkDeployMgr zkDeployMgr = GuiceDI.getInstance(ZkDeployMgr.class);

    /**
     * 获取Zookeeper地址
     *
     * @return
     */
    @NoAuth
    @RequestMapping(value = "/hosts", method = RequestMethod.GET)
    @ResponseBody
    @Path("/hosts")
    public ActionResult getHosts() {

        try {
            ValueVo confItemVo = new ValueVo();
            confItemVo.setStatus(Constants.OK);
            confItemVo.setValue(zooConfig.getZooHosts());

            return new JsonActionResult(confItemVo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取ZK prefix
     *
     * @return
     */
    @NoAuth
    @RequestMapping(value = "/prefix", method = RequestMethod.GET)
    @ResponseBody
    @Path("/prefix")
    public ActionResult getPrefixUrl() {

        try {
            ValueVo confItemVo = new ValueVo();
            confItemVo.setStatus(Constants.OK);
            confItemVo.setValue(zooConfig.getZookeeperUrlPrefix());

            return new JsonActionResult(confItemVo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取ZK 部署情况
     *
     * @param zkDeployForm
     *
     * @return
     */
    @RequestMapping(value = "/zkdeploy", method = RequestMethod.GET)
    @ResponseBody
    @Path("/zkdeploy")
    public ActionResult getZkDeployInfo(@Valid ZkDeployForm zkDeployForm) {

        try {
            PrintBeat.printBeat(beat);

            ConfigFullModel configFullModel = zkDeployValidator.verify(zkDeployForm);

            String data = zkDeployMgr.getDeployInfo(configFullModel.getApp().getName(), configFullModel.getEnv().getName(), zkDeployForm.getVersion());

            return new JsonActionResult(buildSuccess("hostInfo", data));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;

    }
}